package Boundry.GenericTools;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class GenericTabel extends JPanel 
{

	private static final long serialVersionUID = 1L;
	private JTable table;
	
	public JTable getTable() {
		return table;
	}

	private int returned_value;
	private DefaultTableModel model;
	private ListSelectionModel cellSelectionModel;
	
	public ListSelectionModel getCellSelectionModel() {
		return cellSelectionModel;
	}

	public GenericTabel(int Width,int Height,String tableName)
	{
		returned_value=-1;	
		this.setSize(Width,Height);

		JPanel tablepanel_  = new JPanel(new BorderLayout());
		tablepanel_.setBounds(0, 0, Width,Height);
		this.add(tablepanel_);
		  
  	  //Create the table and enable to add data to table
  	   model = new DefaultTableModel(); 
  	   table = new JTable(model) {
  	      /**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public boolean isCellEditable(int rowIndex, int vColIndex) {
  	        return false;
  	      }
		public boolean getScrollableTracksViewportWidth()
        {
            return getPreferredSize().width < getParent().getWidth();
        }
 
  	    };
  	  table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
  	  table.getTableHeader().setReorderingAllowed(false);
  	  
  	  table.setGridColor(Color.black);
      JScrollPane scrollPane = new JScrollPane(table);
		

	  tablepanel_.setBorder(BorderFactory.createTitledBorder( tableName));
		
      scrollPane.setPreferredSize(new Dimension(Width-10,Height-10));   
      tablepanel_.add(scrollPane, BorderLayout.CENTER);
        

         

		// Set listener to the row choose input
		cellSelectionModel = table.getSelectionModel();    
		cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		cellSelectionModel.addListSelectionListener(new ListSelectionListener() 
		    {
			@Override 
			public void valueChanged(ListSelectionEvent e) 
		      {
		    	  returned_value=table.getSelectedRow(); 	  	    
		      }
		  });
		
		 
	}
	
	public void buildTabel(String[] columnNames, String[][] tableData)
	{   
		if(model.getRowCount()>0) removeAllRows();
		if(columnNames==null || tableData==null ) return;
		int length = columnNames.length;
		for(int i=0;i<length;i++)
			addColumn(columnNames[i]); 
		
		length = tableData.length;
		for(int i=0;i<length;i++)
			addRow(tableData[i]);

    	System.out.println("In new Table");
	}
	
	public void addRow(String[] row)
	{
		 model.addRow(row);	
	}
	
	public void addColumn(String colName)
	{
		model.addColumn(colName);
	}
	
	public void removeRow(int rowLineNumber)
	{
		if(model.getRowCount()>rowLineNumber)
		{
		   model.removeRow(rowLineNumber);
		}
	}
	
	public void removeAllRows()
	{
		int numberOfRows= model.getRowCount();
        
		for(int i = 0;i<numberOfRows;i++)
        {
            model.removeRow(0);
        } 
	}
	
    public boolean isChosen() 
    {
    	if (this.returned_value < 0 ) return false;
    	return true;
    }
    
    public int getChosen()
    {
    	 return this.returned_value;
    }
   
    public void clearSelection()
    {
    	if(returned_value != -1)
    	{
    		table.getSelectionModel().clearSelection();
    		returned_value = -1;
    	}
    }
    
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame frame = new JFrame();
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setBounds(100, 100, 300, 300);
					GenericTabel b = new GenericTabel(200,200,"Genric ....");
					frame.getContentPane().add(b);
					frame.setVisible(true);
					
			    	String[] columnNames2 = { "number", "name", "status","Command" };
			  	    String[][] tableData2 = {{"1",     "avi",     "done"    , "11234" },
				                                {"2",   "dos",     "dofgdfgdfsdsfdsfne" , "11234"    },
				                                {"3",  "tres",    "ddsfdsfdsone"    , "11234" },
				                                { "4", "cuatro",  "done", "11234"},
				                                { "5",   "cinco",   "done" , "11234"},
				                                { "6",    "seis",    "done"   , "11234" },
				                                { "7",   "siete",   "done" , "11234" } ,
				                                { "8",   "siete",   "done"  , "11234"},
				                                { "9",   "siete",   "done"  , "11234"},
				                                { "10",   "siete",   "done"  , "11234"},
				                                { "11",   "siete",   "done"  , "11234"},
				                                { "12",   "siete",   "done"  , "11234"},
				                                { "13",   "siete",   "done"  , "11234"},
				                                { "14",   "siete",   "done"  , "11234"},
				                                { "7",   "siete",   "done" , "11234" } ,
				                                { "8",   "siete",   "done"  , "11234"},
				                                { "9",   "siete",   "done"  , "11234"},
				                                { "10",   "siete",   "done"  , "11234"},
				                                { "11",   "siete",   "done"  , "11234"},
				                                { "12",   "siete",   "done"  , "11234"},
				                                { "13",   "siete",   "done"  , "11234"},
				                                { "14",   "siete",   "done"  , "11234"},
				                                { "7",   "siete",   "done" , "11234" } ,
				                                { "8",   "siete",   "done"  , "11234"},
				                                { "9",   "siete",   "done"  , "11234"},
				                                { "10",   "siete",   "done"  , "11234"},
				                                { "11",   "siete",   "done"  , "11234"},
				                                { "12",   "siete",   "done"  , "11234"},
				                                { "13",   "siete",   "done"  , "11234"},
				                                { "14",   "siete",   "done"  , "11234"},
				                                { "7",   "siete",   "done" , "11234" } ,
				                                { "8",   "siete",   "done"  , "11234"},
				                                { "9",   "siete",   "done"  , "11234"},
				                                { "10",   "siete",   "done"  , "11234"},
				                                { "11",   "siete",   "done"  , "11234"},
				                                { "12",   "siete",   "done"  , "11234"},
				                                { "13",   "siete",   "done"  , "11234"},
				                                { "14",   "siete",   "done"  , "11234"},
				                                { "15",   "siete",   "done"  , "333333"}};
			  	   // Thread.sleep(5000);
			  	    b.buildTabel(columnNames2, tableData2);			  	    
			  	    
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
